package com.zfzs.post.modules.agentinfo.service.impl;

import com.alibaba.fastjson.JSON;
import com.zfzs.post.exception.BadRequestException;
import com.zfzs.post.model.dto.QueryFindAgentInfoPageDTO;
import com.zfzs.post.model.dto.UpdateAgentInfoDTO;
import com.zfzs.post.modules.agent.domain.Agent;
import com.zfzs.post.modules.agent.domain.AgentMoney;
import com.zfzs.post.modules.agent.service.AgentMoneyService;
import com.zfzs.post.modules.agent.service.AgentService;
import com.zfzs.post.modules.agentinfo.dto.ChannelDTO;
import com.zfzs.post.modules.agentinfo.service.AgentWebService;
import com.zfzs.post.modules.system.domain.User;
import com.zfzs.post.modules.system.service.UserService;
import com.zfzs.post.modules.system.service.dto.UserDTO;
import com.zfzs.post.page.JpaPageHelper;
import com.zfzs.post.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;


/**
 * 代理商实现类
 *
 * @author ck
 * @date 2018-11-23
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class AgentWebServiceImpl implements AgentWebService {

    @Autowired
    private AgentService agentService;

    @Autowired
    private AgentMoneyService moneyService;

    @Autowired
    private UserService userService;


    @Override
    public Map<String, Object> getAgentInfoPage(QueryFindAgentInfoPageDTO criteria, Integer page, Integer size) {
        Map<String, Object> map = new HashMap<>();
        //获取渠道ID
        Long id = SecurityUtils.getUserId();
        //管理员数据
        if (!id.equals(1L)) {
            criteria.setChannelId(id);
        }
        JpaPageHelper pageHelper = agentService.queryAgentInfoPageList(criteria, page, size);
        map.put("content", pageHelper.getResult());
        map.put("totalElements", pageHelper.getTotalCount());
        return map;
    }

    @Override
    public void updateAgentInfo(UpdateAgentInfoDTO dto) {
        //插叙您当前人登录人手机号码
        //UserDetails userDetails = SecurityUtils.getUserDetails();
        //修改基础信息
        Integer isEffective = dto.getIsEffective();
        Long isWithdraw = dto.getIsWithdraw();
        String phone = dto.getPhone();
        Long status = dto.getStatus();
        BigDecimal frozenMoney = dto.getFrozenMoney();
        BigDecimal usableMoney = dto.getUsableMoney();
        Long id = dto.getId();
        //校验数据
        Agent agent = agentService.findById(id);
        if (agent == null) {
            throw new BadRequestException("编号" + id + "代理商不存在");
        }
        //是否修改了手机号码
        if (!agent.getPhone().equals(phone)) {
            //查询手机号码是否存在
            Agent a = agentService.findByPhoneAndChannelId(phone, agent.getChannelId());
            if (a != null) {
                throw new BadRequestException("手机号码已经被注册");
            }
        }
        //修改提现状态
        AgentMoney money = moneyService.findById(id);
        if (money == null) {
            throw new BadRequestException("代理商" + id + "账户不存在");
        }
        //校验密码
        agent.setPhone(phone);
        agent.setIsEffective(isEffective);
        agent.setStatus(status);
        agentService.update(agent);
        money.setUsableMoney(usableMoney);
        money.setFrozenMoney(frozenMoney);
        money.setStatus(isWithdraw);
        moneyService.update(money);
    }

    @Override
    public void deleteAgentInfo(Long id) {
        Agent agent = agentService.findById(id);
        if (agent == null) {
            throw new BadRequestException("编号" + id + "代理商不存在");
        }
        agent.setDelFlag(true);
        agentService.update(agent);
    }

    @Override
    public void updateChannel(ChannelDTO dto) {
        String email = dto.getEmail();
        Long id = dto.getId();
        String nickname = dto.getNickname();
        String phone = dto.getPhone();
        //查询是否存在
        UserDTO userDto = userService.findById(id);
        if (userDto == null) {
            throw new BadRequestException("未查询到渠道信息");
        }
        userDto.setPhone(phone);
        userDto.setEmail(email);
        userDto.setNickname(nickname);
        User user = JSON.parseObject(JSON.toJSONString(userDto), User.class);
        userService.update(user);
    }
}
